﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace BHair.Report
{
    public partial class frmxiangmuxiaofeihuizong : WeifenLuo.WinFormsUI.Docking.DockContent
    {
        public frmxiangmuxiaofeihuizong()
        {
            InitializeComponent();
        }
        string constr = ConfigurationManager.ConnectionStrings["BHair.Properties.Settings.BHairDBConnectionString"].ConnectionString;
        private void button1_Click(object sender, EventArgs e)
        {
            //首先查询出项目名称
            SqlConnection conn = new SqlConnection(constr);
            SqlConnection conn1 = new SqlConnection(constr);
            conn.Open();
            conn1.Open();
            string sql = "SELECT [iid] ,[iname] FROM [BHairDB].[dbo].[items] where jc=1";
            SqlCommand comm = new SqlCommand(sql,conn);
            int cs;
            string fl;
            SqlDataReader sdr =comm.ExecuteReader();
            var dt = new DataTable();
            dt.Columns.Add(new DataColumn("项目", typeof(string)));
            dt.Columns.Add(new DataColumn("次数", typeof(int)));
            while (sdr.Read())
            {
                fl = sdr["iname"].ToString();
                //根据项目名称实现统计
                string sql2 = string.Format("SELECT sum([xfcs]) FROM [BHairDB].[dbo].[jcxf] join  items on jcxf.xmid=items.iid where xmname='{0}'and riqi>='{1}' and riqi<='{2}'", fl,dateTimePicker1.Text,dateTimePicker2.Text);
                SqlCommand comm2 = new SqlCommand(sql2,conn1);
                if (comm2.ExecuteScalar().ToString() != string.Empty)
                {
                    cs = Int32.Parse(comm2.ExecuteScalar().ToString());
                }
                else
                {
                    cs = 0;
                }
                var row = dt.NewRow();
                row["项目"] = fl;
                row["次数"] = cs;
                dt.Rows.InsertAt(row, 0);

            }
            dataGridView1.DataSource = dt;
            conn.Close();
            conn1.Close();
        }

        private void frmxiangmuxiaofeihuizong_Load(object sender, EventArgs e)
        {

        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
       e.RowBounds.Location.Y,
       dataGridView1.RowHeadersWidth - 4,
       e.RowBounds.Height);

            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
            dataGridView1.RowHeadersDefaultCellStyle.Font,
                rectangle,
                dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }
    }
}
